<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\App;

class PictureResource extends JsonResource
{
  protected $formats;

  public function toArray($request)
  {
    $this->formats = config('coreapi.' . App::getLocale());

    return [
      'id' => $this->id,
      'title' => $this->title,
      'filename' => $this->filename,
      'original_filename' => $this->original_filename,
      'mime_type' => $this->mime_type,
      'size' => $this->size,
      'image' => ($this->filename ? base64_encode(file_get_contents($this->filename)) : null),
      'active' => $this->active,
      'created_at' => localizedFormat($this->created_at, 'datetime'),
      'updated_at' => localizedFormat($this->updated_at, 'datetime'),
      'user' => new UserResource($this->whenLoaded('user')),
      'type' => new TypeResource($this->whenLoaded('type')),
    ];
  }
}
